package com.sw.paisong.controller;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.sw.paisong.service.PosService;
import com.sw.paisong.service.StaffService;
import com.sw.paisong.util.ServletUtils;
import com.sw.paisong.vo.JsonVO;
import com.sw.paisong.vo.mob.HandshakeFormVO;
import com.sw.paisong.vo.mob.ReportPosFormVO;

@Controller
@RequestMapping(value = "/mob")
public class MobController {

	@Autowired
	StaffService staffService;

	@Autowired
	PosService posService;

	@RequestMapping(value = "/handshake", method = RequestMethod.POST)
	public void handshake(HandshakeFormVO handshakeForm, HttpServletResponse response) {
		boolean isSuccess = true;
		String message = "";
		if (handshakeForm == null) {
			isSuccess = false;
			message = "参数不能为空";
		} else if (StringUtils.isBlank(handshakeForm.getStaffNum())) {
			isSuccess = false;
			message = "工号不能为空";
		} else if (StringUtils.isBlank(handshakeForm.getUsername())) {
			isSuccess = false;
			message = "用户名不能为空";
		} else if (StringUtils.isBlank(handshakeForm.getPhone())) {
			isSuccess = false;
			message = "手机号码不能为空";
		} else if (StringUtils.isBlank(handshakeForm.getVPhone())) {
			isSuccess = false;
			message = "虚拟号不能为空";
		}
		if (isSuccess) {
			ServletUtils.responseJson(response, staffService.handshake(handshakeForm));
			return;
		} else {
			JsonVO<HandshakeFormVO> json = new JsonVO<HandshakeFormVO>();
			json.setMessage(message);
			json.setSuccess(isSuccess);
			ServletUtils.responseJson(response, json);
		}
	}

	@RequestMapping(value = "/pos/report", method = RequestMethod.POST)
	public void reportPos(ReportPosFormVO reportPosForm, HttpServletResponse response) {
		boolean isSuccess = true;
		String message = "";
		if (reportPosForm == null) {
			isSuccess = false;
			message = "参数不能为空";
		} else if (StringUtils.isBlank(reportPosForm.getToken())) {
			isSuccess = false;
			message = "token不鞥为空";
		} else if (reportPosForm.getLat() == null) {
			isSuccess = false;
			message = "纬度不鞥为空";
		} else if (reportPosForm.getLng() == null) {
			isSuccess = false;
			message = "经度不鞥为空";
		}

		if (isSuccess) {
			ServletUtils.responseJson(response, posService.reportPos(reportPosForm));
			return;
		} else {
			JsonVO<HandshakeFormVO> json = new JsonVO<HandshakeFormVO>();
			json.setMessage(message);
			json.setSuccess(isSuccess);
			ServletUtils.responseJson(response, json);
		}
	}

}
